package com.ebpm.designer.line
{
	import com.ebpm.designer.WF;
	import com.ebpm.designer.WP;
	import com.ebpm.designer.WorkPanel;
	import com.ebpm.designer.dir.LineEndDir;
	import com.ebpm.designer.dir.LineFromDir;
	import com.ebpm.designer.dir.LineMiddleDir;

	/**
	 * 
	 * 底部节点 往下的线
	 * 
	 **/
	public class DDLine extends Line
	{
		public var p1:Point;
		public var p2:Point;
		public var startP1:Point;
		public var startP2:Point;
		public function DDLine()
		{
			this.resizeFromDir = new LineFromDir(this, "leftRightIcon");
			this.resizeToDir = new LineEndDir(this, "leftRightIcon");
			this.middleDir = new LineMiddleDir(this, "upDownIcon");
			this.lineHeadType = "up";
			super();
		}
		
		override protected function initPoints(fromX:Number, fromY:Number, x:Number, y:Number):void {
			var itemSize:Number = WF.ITEM_SIZE;
			var showY:Number = y;
			if(y < fromY) {
				showY = fromY;
			}
			p1 = new Point(fromX + (itemSize / 2) + this.resizeFromExtraX, showY + WF.LINE_EXT_SIZE + (itemSize / 2) + this.resizeMiddleExtra);
			p2 = new Point(x + (itemSize / 2) + this.resizeToExtraX, this.p1.y);
			this.points.addItem(p1);
			this.points.addItem(p2);
		}
		
		override public function repaintLine(type:String = "to"):void {
			var fromX:Number = this.sourceItem.nodeContainer.getDownLineX();
			var fromY:Number = this.sourceItem.nodeContainer.getDownLineY();
			var toX:Number = this.targetItem.nodeContainer.getDownLineX();
			var toY:Number = this.targetItem.nodeContainer.getDownLineY();
			//先将list中所有的点remove掉
			this.points.removeAll();
			//判断是否越界，是的画切换线
			if(
				(this.sourceItem.nodeContainer.x < this.targetItem.nodeContainer.x && this.sourceItem.nodeContainer.y > this.targetItem.nodeContainer.y && this.sourceItem.nodeContainer.x + this.sourceItem.nodeContainer.width >= this.endPoint.x) || 
				(this.sourceItem.nodeContainer.x < this.targetItem.nodeContainer.x && this.sourceItem.nodeContainer.y < this.targetItem.nodeContainer.y && this.startPoint.x >= this.targetItem.nodeContainer.x) || 
				(this.sourceItem.nodeContainer.x > this.targetItem.nodeContainer.x && this.sourceItem.nodeContainer.y > this.targetItem.nodeContainer.y && this.sourceItem.nodeContainer.x <= this.endPoint.x) ||
				(this.sourceItem.nodeContainer.x > this.targetItem.nodeContainer.x && this.sourceItem.nodeContainer.y < this.targetItem.nodeContainer.y && this.targetItem.nodeContainer.x + this.targetItem.nodeContainer.width >= this.startPoint.x)                            
			) {
				///判断 fromItem 和 toItem 哪个坐标y高，如果fromItem高，切换为DTLine
				WF.wp.removeElement(this);
				this.sourceItem.sourceLines.removeItem(this);
				this.targetItem.targetLines.removeItem(this);
				if(this.sourceItem.nodeContainer.getDownLineY() < this.targetItem.nodeContainer.getDownLineY()) {
					var line:Line = new LLLine().paint(this.sourceItem.nodeContainer.getLeftLineX(), this.sourceItem.nodeContainer.getLeftLineY(),
						this.targetItem.nodeContainer.getLeftLineX(), this.targetItem.nodeContainer.getLeftLineY());
					line.txtLabel.text = this.txtLabel.text;
					line.txtInput.text = this.txtInput.text;
					line.showLabel();
					this.targetItem.nodeContainer.leftItem.targetLines.addItem(line);
					line.targetItem = this.targetItem.nodeContainer.leftItem;
					this.sourceItem.nodeContainer.leftItem.sourceLines.addItem(line);
					line.sourceItem = this.sourceItem.nodeContainer.leftItem;
				} else {
					var line:Line = new RRLine().paint(this.sourceItem.nodeContainer.getRightLineX(), this.sourceItem.nodeContainer.getRightLineY(),
						this.targetItem.nodeContainer.getRightLineX(), this.targetItem.nodeContainer.getRightLineY());
					line.txtLabel.text = this.txtLabel.text;
					line.txtInput.text = this.txtInput.text;
					line.showLabel();
					this.targetItem.nodeContainer.rightItem.targetLines.addItem(line);
					line.targetItem = this.targetItem.nodeContainer.rightItem;
					this.sourceItem.nodeContainer.rightItem.sourceLines.addItem(line);
					line.sourceItem = this.sourceItem.nodeContainer.rightItem;
				}
			}
			else if(!isAlreadyResize && fromY >= toY) 																														//没有resize 线之前，越界了，p1、p2的Y不动
			{
				if(type == "to") {
					toOverPaint(fromX, fromY, toX, toY);
				} else {
					fromOverPaint(fromX, fromY, toX, toY);
				}
			}
			else 
			{
				//如果没有resize 线，重画线
				if(!isAlreadyResize) {
					paint(fromX, fromY, toX, toY);
				} else { //如果resize过了线
					//toItem节点移动时触发
					if(this.startPoint.y >= this.p1.y) {  //判断线是否向下越界了
						toOverPaint(fromX, fromY, toX, toY);
						isAlreadyResize = false;
					} 
					else if(this.endPoint.y + WF.LINE_HEAD_W >= this.p2.y) {
						paint(fromX, fromY, toX, toY);
						isAlreadyResize = false;
					} 
					else {
						toOverPaint(fromX, fromY, toX, toY);
					}
				}
			}
			
		}
		
		public function fromOverPaint(fromX:Number, fromY:Number, x:Number, y:Number):void {
			var itemSize:Number = WF.ITEM_SIZE;
			startPoint = new Point(fromX + (itemSize / 2) + this.resizeFromExtraX, fromY + (itemSize / 2));
			this.points.addItem(startPoint);
			p1 = new Point(fromX + (itemSize / 2) + this.resizeFromExtraX, fromY + WF.LINE_EXT_SIZE + (itemSize / 2));
			p2 = new Point(x + (itemSize / 2) + this.resizeToExtraX, fromY + WF.LINE_EXT_SIZE + (itemSize / 2));
			this.points.addItem(p1);
			this.points.addItem(p2);
			endPoint = new Point(x + (itemSize / 2) + this.resizeToExtraX, y + (itemSize / 2));
			this.points.addItem(endPoint);
			this.drawLine();
			//如果重新画整条线的话,必须把resizeMiddleExtra设置为0
			this.resizeMiddleExtra = 0;
		}
		
		//线移动过后，该方法保持 p1、p2的坐标Y不变
		public function toOverPaint(fromX:Number, fromY:Number, x:Number, y:Number):void {
			var itemSize:Number = WF.ITEM_SIZE;
			startPoint = new Point(fromX + (itemSize / 2) + this.resizeFromExtraX, fromY + (itemSize / 2));
			this.points.addItem(startPoint);
			//p1、p2的 坐标Y保持不变
			p1 = new Point(fromX + (itemSize / 2) + this.resizeFromExtraX, this.p1.y);
			p2 = new Point(x + (itemSize / 2) + this.resizeToExtraX, this.p2.y);
			this.points.addItem(p1);
			this.points.addItem(p2);
			endPoint = new Point(x + (itemSize / 2) + this.resizeToExtraX, y + (itemSize / 2));
			this.points.addItem(endPoint);
			this.drawLine();
			//如果重新画整条线的话,必须把resizeMiddleExtra设置为0
			this.resizeMiddleExtra = 0;
		}
		
		override public function resizeMiddleItem():void {
			if(this.isFirstMove) {
				this.startP1 = this.p1;
				this.startP2 = this.p2;
				this.isFirstMove = false;
				this.resizeStartExtra = this.resizeMiddleExtra;
			}
//			if(p2.y - this.toItem.userTaskContainer.getDownLineY() <= WF.LINE_EXT_SIZE) {
//				return ;
//			}
			this.resizeMiddleExtra = this.resizeStartExtra + WP.mouseChangeY;
			this.points.removeAll();
			this.points.addItem(this.startPoint);
			this.p1 = new Point(this.startP1.x, this.startP1.y + WP.mouseChangeY);
			this.p2 = new Point(this.startP2.x, this.p1.y);
			this.points.addItem(this.p1);
			this.points.addItem(this.p2);
			this.points.addItem(this.endPoint);
			this.drawLine();
			this.middleDir.y = this.p1.y - (WF.ITEM_SIZE / 2);
		}
		
		//拖拽框移动
		override public function resizeFromItem():void {
			if(this.isFirstMove) {
				this.resizeStartExtra = this.resizeFromExtraX;
				this.isFirstMove = false;
			}
			this.resizeFromExtraX = this.resizeStartExtra + WP.mouseChangeX;
			this.points.removeAll();
			var fromX:Number = this.sourceItem.nodeContainer.getDownLineX();
			var fromY:Number = this.sourceItem.nodeContainer.getDownLineY();
			var toX:Number = this.targetItem.nodeContainer.getDownLineX();
			var toY:Number = this.targetItem.nodeContainer.getDownLineY();
			this.toOverPaint(fromX, fromY, toX, toY);
		}
		
		
		override public function resizeEndItem():void {
			if(this.isFirstMove) {
				this.resizeStartExtra = this.resizeToExtraX;
				this.isFirstMove = false;
			}
			this.resizeToExtraX = this.resizeStartExtra + WP.mouseChangeX;
			this.points.removeAll();
			var fromX = this.sourceItem.nodeContainer.getDownLineX();
			var fromY = this.sourceItem.nodeContainer.getDownLineY();
			var toX = this.targetItem.nodeContainer.getDownLineX();
			var toY = this.targetItem.nodeContainer.getDownLineY();
			this.toOverPaint(fromX, fromY, toX, toY);
			this.movingTxtByLine();
		}
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
	}
}